package kr.co.mustgo.controller.noticeboard;

import java.util.Map;

import kr.co.mustgo.domain.noticeboard.NoticeBoard;
import kr.co.mustgo.model.service.noticeboard.NoticeBoardService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/noticeboard/")
public class NoticeBoardController {

	@Autowired
	private NoticeBoardService noticeBoardService;
	
	@RequestMapping("write_form.do")
	public String writeForm(){
		return "tiles3/noticeBoard/write_form.jsp";
	}
	
	@RequestMapping("writeContent.do")
	public ModelAndView writeContent(NoticeBoard noticeBoard){
		ModelAndView mv = null;
		noticeBoardService.writeContent(noticeBoard);
		mv = new ModelAndView("redirect:getContent.do?code="+noticeBoard.getCode());
		
		return mv;
	}
	
	@RequestMapping("getContent.do")
	public String getContent(int code, ModelMap map){
		String url = null;
		try{
			//2. Business Logic �슂泥� - Model
			NoticeBoard noticeBoard = noticeBoardService.getContentByCode(code);
			map.addAttribute("noticeBoard", noticeBoard);
			//System.out.println("map" + noticeBoard);
			url = "tiles3/noticeBoard/show_content.jsp";
		}catch(Exception e){
			e.printStackTrace();
			map.addAttribute("error_message", "글 조회도중 오류 발생 : "+e.getMessage());
			url = "tiles2/error/error.jsp";
		}	
		return url;
	}
	
	
	@RequestMapping("list.do")
	public String list(@RequestParam(defaultValue="1")String page, ModelMap model) throws Exception{
		
		String url = null;
		//2. Business Logic 호출 ->model		
			//page=  로 넘어온 경우(위 @RequestParam 설정은 page 요청파라미터 이름으로 넘어온 것이 없는 경우이다.
		int pageNo = 1;
		try{
			pageNo = Integer.parseInt(page);
		}catch(Exception e){}
		Map map = noticeBoardService.getNoticeBoardList(pageNo);
		model.addAllAttributes(map);
	//	System.out.println(map);
		url = "tiles3/noticeBoard/list.jsp";
		return url;
	}
	
	@RequestMapping("modifyForm.do")
	public String modifyForm(int code, ModelMap map){
		String url = null;
		try{
			//2. B.L 처리
			NoticeBoard noticeBoard = noticeBoardService.getContentByNoForForm(code);
			url = "tiles3/noticeBoard/modify_form.jsp";
			map.addAttribute("noticeBoard", noticeBoard);
	
		}catch(Exception e){
			e.printStackTrace();
			url = "tiles2/error/error.jsp";
			map.addAttribute("error_message", "수정할 글 조회도중 오류발생 : "+e.getMessage());
		}
		return url;
	}
	
	@RequestMapping("modifyContent.do")
	public ModelAndView modifyContent(NoticeBoard noticeBoard, int page){
		ModelAndView mv = null;
		try{
			System.out.println(noticeBoard);
			noticeBoardService.modifyContent(noticeBoard);
			System.out.println(noticeBoard);
			mv = new ModelAndView("redirect:getContent.do?code="+noticeBoard.getCode()+"&page="+page);
		}catch (Exception e){
			e.printStackTrace();
			mv = new ModelAndView("tiles2/error/error.jsp");
		}
		return mv;
	}
	
	@RequestMapping("deleteContent.do")
	public String deleteContent(int code, int page, ModelMap map){
		String url = null;
		try {
			//2. BL
			noticeBoardService.deleteContentByCode(code);
			url = "redirect:list.do?page="+page;
		}catch (Exception e) {
			e.printStackTrace();
			url = "tiles2/error/error.jsp";
			map.addAttribute("error_message", "삭제도중 오류 발생 : "+e.getMessage());
		}
		return url;
	}
	
}
